<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>对象的深克隆</title>
</head>
<body>
<script>
    var obj1 = {
        a: 1,
        b: 2,
        c: [33, 44, {
            m: 55,
            n: 66,
            p: [77, 88]
        }]
    };

    //深克隆
    function deepClone(arr) {
        //要判断arr是对象还是数组
        if (Array.isArray(arr)) {
            var result = [];
            for (var i = 0; i < arr.length; i++) {
                result.push(deepClone(arr[i]));
            }
        } else if (typeof arr == 'object') {
            //对象
            var result = {};
            for (var k in arr) {
                result[k] = deepClone(arr[k]);
            }
        } else {
            //基本类型
            var result = arr;
        }
        return result;
    }

    //测试一下
    var obj2 = deepClone(obj1);
    console.log(obj2);

    console.log(obj1.c == obj2.c);

    obj1.c.push(99);
    console.log(obj2.c);
    console.log(obj1.c);

    
</script>
</body>
</html>